import chat from "../assets/icon/chat.png";
import chatAct from "../assets/icon/chat-act.png";
import contact from "../assets/icon/contact.png";
import contactAct from "../assets/icon/contact-act.png";
import group from "../assets/icon/group.png";
import groupAct from "../assets/icon/group-act.png";
import crypto from './crypto';
import store from '../store';
import { getOtherUser } from './getOtherInfo';

export const routeList = [
  {
    id: 1,
    name: '聊天',
    path: '/chat',
  },
  {
    id: 2,
    name: '通讯录',
    path: '/book',
  },
  {
    id: 3,
    name: '个人中心',
    path: '/profile',
  },
];

export const iconList = [
  {
    type: "chat",
    defaultImg: chat,
    actImg: chatAct,
  },
  {
    type: "contact",
    defaultImg: contact,
    actImg: contactAct,
  },
  {
    type: "group",
    defaultImg: group,
    actImg: groupAct,
  },
];

export const getLocalStorage = (key: string) => {
  return localStorage.getItem(key);
};

const encrypt_key = 'xStHXRd7w9j4UzDc';
const iv = 'FZaW6ABCvqKm9VRE';

export const encryptStr = (str: string) => {
  return crypto.encrypt(str, encrypt_key, iv);
}

export const decryptStr = (str: string) => {
  return crypto.decrypt(str, encrypt_key, iv);
};

export const getUserInfo = () => {
  const userInfoBase = localStorage.getItem('userInfo');
  const userInfo = userInfoBase ? JSON.parse(userInfoBase) : {};
  return userInfo;
};

export const getMid = (str: string) => {
  return str.startsWith('jj_') ? str : `jj_${str}`;
};

export const getFileUrl = (url: string) => {
  return url && url.startsWith('//a1.easemob.com')
    ? url.replace('//a1.easemob.com', 'http://www.yanshisites.top') : url;
};

export const setImMsg = async (message: any, type: string) => {
  const {
    getState,
    dispatch
  } = store;
  const state = getState();
  const {
    localMessageList
  } = state?.msg;
  const { chatType } = message;
  const key = chatType === 'singleChat' ? `${message.to}-${message.from}` : message.to;
  const msgList = localMessageList[key] || [];
  const info: any = await getOtherUser(message.from, chatType);
  console.log('收到消息', message);
  const list = msgList.concat({
    ...message,
    source: "message",
    avatar: info.avatar,
    type,
    message: message.msg,
  });
  dispatch({
    type: "ADD_MESSAGE",
    key,
    list
  });
};